package io.renren.modules.business.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import io.renren.modules.sys.controller.AbstractController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.renren.modules.business.entity.EAttendanceEntity;
import io.renren.modules.business.service.EAttendanceService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 考勤表
 *
 * @author ${author}
 * @email ${email}
 * @date 2022-02-24 15:28:33
 */
@RestController
@RequestMapping("/business/eattendance")
public class EAttendanceController extends AbstractController {

    @Autowired
    private EAttendanceService eAttendanceService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = eAttendanceService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		EAttendanceEntity eAttendance = eAttendanceService.getById(id);

        return R.ok().put("eAttendance", eAttendance);
    }

    /**
     * 查看考勤列表
     * @param pageNum
     * @param pageSize
     * @param userId
     * @param departmentId
     * @param clockTime
     * @return
     */
    @GetMapping("/query")
    public R queryAttendanceList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                 @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                 @RequestParam(value = "userId", required = false) Long userId,
                                 @RequestParam(value = "departmentId", required = false) Long departmentId,
                                 @RequestParam(value = "clockTime", required = false) String clockTime) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        if (clockTime == null) {
            clockTime = simpleDateFormat.format(new Date());
        }
        Date date = null;
        try {
            date = simpleDateFormat.parse(clockTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (getUserAuthority() != 1) {
            return R.ok().put("page", eAttendanceService.queryAttendanceList(pageNum, pageSize, getUserId(), departmentId, date));
        }
        return R.ok().put("page", eAttendanceService.queryAttendanceList(pageNum, pageSize, userId, departmentId, date));
    }


    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody EAttendanceEntity eAttendance){
		eAttendanceService.save(eAttendance);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody EAttendanceEntity eAttendance){
		eAttendanceService.updateById(eAttendance);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestParam(value = "id") Long id){
		eAttendanceService.removeById(id);

        return R.ok();
    }

}
